import re
from anole.utils.URL import URL


VIDEO_TYPES=["rm", "rmvb","ra", "avi", "asf", "wmv", "mpg", "mpe", "mpeg",  "mpv", \
	"mov", "qt", "ram", ]
AUDIO_TYPES=["wav", "mp3", "mid", "wma", "mp2", "mp1", "aif", "au", "snd", "cda", \
	"mpl", "m3u", "mjf", "as", "voc", "xm", "s3m", "stm", "mod", "dsm", \
	"far", "ult", "mtm", "mpga", "mpa",]
IMAGE_TYPES=["gif", "ai", "jpg", "tif", "png", "jpe", "icojpg", "bmp", "ico", "jpeg", \
	"pcx", "tiff", "wmf", "psd", "tga", "pic", "pcd", "dib", "rle", "iff", \
	"lbm", "jif", "dcx",]
MEDIA_TYPES=IMAGE_TYPES+VIDEO_TYPES+AUDIO_TYPES
COMPRESS_TYPES=["tar", "zip", "gtar", "ustar", "gz", "z", "arj", "rar",]
BINARY_TYPES=["exe","bin", "lib", "a", "o", "dll",'so',"java", "reg", "jav", "asm",]
OTHER_TYPES=["latex",  "css", 'xsl','xslt','torrent']

NONTEXT_TYPES=MEDIA_TYPES+COMPRESS_TYPES+BINARY_TYPES+OTHER_TYPES

DOC_TYPES=['txt','doc','pdf','ppt']
HTML_TYPES=['html','htm','shtml','shtm','xml']
SCRIPT_TYPES=['jsp','asp','aspx','php','php3','pl','cgi']

ALL_TYPES=MEDIA_TYPES+COMPRESS_TYPES+BINARY_TYPES+OTHER_TYPES+DOC_TYPES+SCRIPT_TYPES

class PatternError(Exception):
    pass
class URLFilter:
    #ALLOW
    #DENY
    #MAX_DEPTH
    #DENY_TYPES
    #DENY_CONTAINS
    def __init__(self,settings=None):
        if settings:
            self.setting(settings)
    def setting(self,settings):
        self._settings=settings
        try:
            self.maxDepth=settings['MAX_DEPTH']
        except KeyError:
            self.maxDepth=0

        try:    
            self.allowPattern=re.compile(settings['ALLOW'])
        except KeyError:
            self.allowPattern=None
        except:
            raise PatternError('invalid patterns: %s'%settings['ALLOW'])            
        try:    
            self.denyPattern=re.compile(settings['DENY'])
        except KeyError:
            self.denyPattern=None
        except:
            raise PatternError('invalid patterns: %s'%settings['DENY'])
        try:
            self.denyTypes=settings['DENY_TYPES']
        except KeyError:
            self.denyTypes=[]
        try:
            self.denyContains=settings['DENY_CONTAINS']
        except KeyError:
            self.denyContains=[]
            
            
    def filter(self,strUrl):
        #shall i be filterd?

	url=URL(strUrl)
        
        #MAX_DPETH test
        if  self.maxDepth and url.depth()>self.maxDepth:
            #print '1'
            return True

        #DENY_CONTAINS test        
        strURL=url.full_url()
        for ct in self.denyContains:
	#'or' test
	    bFilter=True	
	    for c in ct:	
		    #'and' test
		    if strURL.find(c)==-1:
			bFilter=False
			break
	    if bFilter:	
            	return True


        #DENY_TYPES test            
        if url.file_type() in self.denyTypes:
            #print '2'
            return True
        
        #ALLOW_PATTERN test
        if self.allowPattern and not self.allowPattern.match(url.full_url()):
            return True

        #DENY_PATTERN test        
        if   self.denyPattern and  self.denyPattern.match(url.full_url()):
            return True            
        return False
    

def test():
    from anole.utils.URL import URL
    settings={'MAX_DEPTH':3, 
              'ALLOW':'http://net\.pku\.edu\.cn',
              'DENY_CONTAINS':[['?'],['sex']],
              'DENY_TYPES':MEDIA_TYPES+COMPRESS_TYPES+BINARY_TYPES+OTHER_TYPES,
              }
    f=URLFilter(settings)
    
    
    u=URL('http://net.pku.edu.cn/1/2/3/')
    #print u.depth()
    assert f.filter(u)==True
    u=URL('http://net.pku.edu.cn/1/2/3/4')
    assert f.filter(u)==True
    u=URL('http://net.pku.edxxu.cn/')
    assert f.filter(u)==True

    u=URL('http://net.pku.edu.cn/a.rmvb')
    assert f.filter(u)==True

    u=URL('http://net.pku.edu.cn/a.mp3')
    assert f.filter(u)==True

    u=URL('http://net.pku.edu.cn/a.jpg')
    assert f.filter(u)==True    

    u=URL('http://net.pku.edu.cn/a.asp2')
    assert f.filter(u)==False

    u=URL('http://net.pku.edu.cn/max.asp?id=3')
    assert f.filter(u)==True
    u=URL('http://net.pku.edu.cn/1/sex/2/3')
    assert f.filter(u)==True 
   
    u=URL('http://net.pku.edu.cn/a.asp')
    #rint u.file_type()
    assert f.filter(u)==False

    u=URL('http://net.pku.edu.cn/a.ASP')
    #print u.file_type()
    assert f.filter(u)==False    

    u=URL('http://net.pku.edu.cn/')
    assert f.filter(u)==False
    
    u=URL('http://net.pku.edu.cn/1/2/3')
    assert f.filter(u)==False

    u=URL('http://net.pku.edu.cn/a.htm')
    assert f.filter(u)==False

    u=URL('http://net.pku.edu.cn/a.html')
    assert f.filter(u)==False

    u=URL('http://net.pku.edu.cn/a.Html')
    assert f.filter(u)==False

    u=URL('http://net.pku.edu.cn/a.HTML')
    assert f.filter(u)==False

    u=URL('http://net.pku.edu.cn/a.shtml')
    assert f.filter(u)==False

    u=URL('http://net.pku.edu.cn/a.shtm')
    assert f.filter(u)==False

    u=URL('http://net.pku.edu.cn/a.other')
    assert f.filter(u)==False    
    #assert False
    print 'all ok'
if __name__=='__main__':
    test()
    
    
